import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: GodK
 * Date: 2024-04-14
 * Time: 17:21
 */
public class Mysort {

    // 插入排序
    public static void insertSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int j = i - 1;
            int tmp = array[i];
            for (; j >= 0; j--) {
                if (array[j] > tmp) {
                    array[j + 1] = array[j];
                } else {
                    break;
                }
            }
            array[j + 1] = tmp;
        }
    }

    // 希尔排序
    public static void shellSort(int[] array) {
        int gap = array.length;
        while (gap > 1) {
            gap = gap / 3 + 1;
            shell(array, gap);
        }
    }

    private static void shell(int[] array, int gap) {
        for (int i = gap; i < array.length; i++) {
            int tmp = array[i];
            int j = i - gap;
            for (; j >= 0; j -= gap) {
                if (array[j] > tmp) {
                    array[j + gap] = array[j];
                } else {
                    break;
                }
            }
            array[j + gap] = tmp;
        }
    }

    // 选择排序
    private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            for (int j = i + 1; j < array.length; j++) {
                if (array[j] < array[i]) {
                    swap(array, i, j);
                }
            }
        }
    }

    // 堆排序
    private static void createBigHeap(int[] array) {
        for (int i = (array.length - 1 - 1) / 2; i >= 0; i--) {
            siftDown(i, array, array.length);
        }
    }

    private static void siftDown(int parent, int[] array, int end) {
        int child = parent * 2 + 1;
        while (child < end) {
            if (child + 1 < end && array[child + 1] > array[child]) {
                child++;
            }
            if (array[parent] < array[child]) {
                swap(array, parent, child);
                parent = child;
                child = 2 * parent + 1;
            } else {
                break;
            }
        }
    }

    public static void heapSort(int[] array) {
        createBigHeap(array);
        int end = array.length - 1;
        while (end >= 0) {
            swap(array, 0, end);
            siftDown(0, array, end);
            end--;
        }
    }

    // 冒泡排序
    public static void bubbleSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            boolean flag = false;
            for (int j = 0; j < array.length - 1 - i; j++) {
                if (array[j] > array[j + 1]) {
                    swap(array, j, j + 1);
                    flag = true;
                }
            }
            if (!flag) {
                break;
            }
        }
    }

    // 快速
    private static int partition(int[] array, int left, int right) {
        int i = left;
        int key = array[left];
        while (left < right) {
            while (left < right && key <= array[right]) {
                right--;
            }
            while (left < right && key >= array[left]) {
                left++;
            }
            swap(array, left, right);
        }
        swap(array, left, i);
        return left;
    }

    private static int midThreeNum(int[] array, int left, int right) {
        int min = (left + right) / 2;
        if (array[left] < array[right]) {
            if (array[left] > array[min]) {
                return left;
            } else if (array[right] < array[min]) {
                return right;
            } else {
                return min;
            }
        } else {
            if (array[left] < array[min]) {
                return left;
            } else if (array[right] > array[min]) {
                return right;
            } else {
                return min;
            }
        }
    }

    public static void quickSort(int[] array) {
        quick(array, 0, array.length - 1);
    }

    private static void quick(int[] array, int left, int right) {
        if (left >= right) {
            return;
        }
        if (right - left + 1 < 10) {
            insertSort(array);
            return;
        }
        int min = midThreeNum(array, left, right);
        swap(array, left, min);
        int par = partition(array, left, right);
        quick(array, left, par - 1);
        quick(array, par + 1, right);
    }

        public static void quickSortNor(int[] array){
        Stack<Integer> stack = new Stack<>();
        int left = 0;
        int right = array.length - 1;
        int par = partition(array,left,right);
        if(par > left + 1){
            stack.push(left);
            stack.push(par - 1);
        }
        if(par < right - 1){
            stack.push(par + 1);
            stack.push(right);
        }
        while(!stack.empty()){
            right = stack.pop();
            left = stack.pop();
            par = partition(array,left,right);
            if(par > left + 1){
                stack.push(left);
                stack.push(par - 1);
            }
            if(par < right - 1){
                stack.push(par + 1);
                stack.push(right);
            }
        }
    }

    //归并排序
    public static void mergeSort(int[] array){
        mergeSortFun(array,0 ,array.length - 1);
    }

    public static void mergeSortFun(int[] array,int left,int right){
        if(left >= right){
            return;
        }
        int mid = (left + right)/2;
        mergeSortFun(array,0,mid);
        mergeSortFun(array,mid + 1,right);
        merge(array,left,mid,right);
    }

    private static void merge(int[] array,int left, int mid,int right){
        int[] tmp = new int[right - left + 1];
        int k = 0;
        int m = left;
        int n = mid + 1;
        while(m <= mid && n <= right){
            if(array[m] > array[n]){
                tmp[k++] = array[n++];
            }else {
                tmp[k++] = array[m++];
            }
        }
        while (m <= mid){
            tmp[k++] = array[m++];
        }
        while (n <= right){
            tmp[k++] = array[n++];
        }
        for (int i = 0; i < k; i++) {
            array[i+left] = tmp[i];
        }
    }

    public static void mergeSortNor(int[] array){
        int gap = 1;
        while(gap < array.length){
            for (int i = 0; i < array.length; i = i + 2*gap) {
                int left = i;
                int mid = left + gap - 1;
                if(mid >= array.length){
                    mid = array.length - 1;
                }
                int right = mid + gap;
                if(right>= array.length){
                    right = array.length - 1;
                }
                merge(array,left,mid,right);
            }
            gap *= 2;
        }
    }
}
